package com.sie.zhq.module;

import android.util.Log;

import com.sie.zhq.constconfig.ConstDefine;
import com.sie.zhq.constconfig.ErrorDefine;
import com.sie.zhq.tool.ByteTool;
import com.sie.zhq.tool.FileUnits;

import java.util.Arrays;

/**
 * Created by zhq on 2016/10/20.
 */
public class WellFingerprintModule extends SerialPortModule {

    public final static String TAG = "WellFingerprintModule";
    private static WellFingerprintModule ourInstance = new WellFingerprintModule();
    private String serialDev;
    private int baudRate = 9600;
    protected boolean isOpen = false;
    public static WellFingerprintModule getInstance() {
        return ourInstance;
    }

    private static final byte HEADER = 0x02;
    private static final byte END = 0x03;
    private static byte[] sendBuffer = new byte[1024];
    private static byte[] revBuffer = new byte[40960];
    private WellFingerprintModule() {

    }
    public boolean init(){
        PowerOn();
        this.serialDev = ConstDefine.FINGERPRINT_PORT;
        if(this.serialDev==null){
            isOpen = false;
            return false;
        }
        if (initSerialPort(this.serialDev, this.baudRate)) {
            Log.e("initserial", "open");
            isOpen = true;
            return true;
        }
        return false;
    }
    public boolean isOpen() {

        return isOpen;
    }
    public void PowerOn() {
        FileUnits.WriteTxtFile("en","/sys/devices/soc/10003000.keypad/elink_fingerprintctrl");
//        GPIO.gpioCrtl(284, 1);
//        GPIO.gpioCrtl(287, 1);
    }
    public void PowerOff() {
        FileUnits.WriteTxtFile("no","/sys/devices/soc/10003000.keypad/elink_fingerprintctrl");
//        GPIO.gpioCrtl(284, 0);
//        GPIO.gpioCrtl(287, 0);
    }
    public int readVersion(byte[] buffer,int[] len){
        byte[] send = new byte[0];
        sendPacket(send,0,(byte) 0x09);
        revPacket(len,2000);

        return 0;
    }


    public int revPacket(int[] pwPacketetLen, int dwTimeoutMs){
        Arrays.fill(revBuffer,(byte)0x00);
        int wDataLen = 0;
        int byRet;
        byRet = RecvBytes(revBuffer, 0, 1, dwTimeoutMs);
        Log.e(TAG, ByteTool.bytearrayToHexString(revBuffer,1));
        if ((1 != byRet) || (2 != revBuffer[0])) {
            return -1;
        }
        byRet = RecvBytes(revBuffer, 1, 2, 200);
        if (2 != byRet) {
            return ErrorDefine.RECV_PACKET_ERROR;
        }
        wDataLen = (revBuffer[1] & 0xFF) * 256 + (revBuffer[2] & 0xFF);
        pwPacketetLen[0] = (short) (wDataLen + 3);
        if (wDataLen > 2176) {
            return -2511;
        }
        byRet = RecvBytes(revBuffer, 3, wDataLen + 2, 500);
        if (wDataLen + 2 != byRet) {
            return ErrorDefine.RECV_PACKET_ERROR;
        }
        Log.e(TAG, ByteTool.bytearrayToHexString(revBuffer,6+wDataLen));
        if(revBuffer[6+wDataLen]!=0x03){
            return ErrorDefine.RECV_PACKET_ERROR;
        }
        pwPacketetLen[0] = wDataLen+5;

        return  0;

    }

    public void sendPacket(byte[] send, int length, byte byModules){
        Arrays.fill(sendBuffer,(byte)0x00);
        sendBuffer[0] = HEADER;
        sendBuffer[1] = (byte) ((length+4) / 256);
        sendBuffer[2] = (byte) ((length+4) % 256);
        sendBuffer[3] = byModules;
        System.arraycopy(send,0,sendBuffer,7,length);
        sendBuffer[length+7] = xor(sendBuffer,1,length+7);
        sendBuffer[length+8] = END;

        Log.e(TAG,"send:"+ ByteTool.bytearrayToHexString(sendBuffer,sendBuffer.length));
        writeFlush(sendBuffer,sendBuffer.length);
    }

    public byte xor(byte[] b,int start,int end){
        byte rs = (byte) ((b[start] & 0xFF) ^ (b[start+1] & 0xFF));
        for(int i=start+2;i<end;i++){
              rs  =  (byte)((rs&0xFF)^(b[i]&0xFF));
        }
        return rs;
    }

}
